package com.li.service.impl;

import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.domain.UploadFile;
import com.li.mapper.UploadFileMapper;
import com.li.service.UploadFileService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UploadFileServiceImpl extends ServiceImpl<UploadFileMapper, UploadFile> implements UploadFileService{

    @Autowired
    private UploadFileMapper uploadFileMapper;
    @Override
    public List<UploadFile> listPageUploadFile(UploadFile uploadFile, Integer page, Integer limit) {
        Integer indexPage=(page-1)*limit;
        return uploadFileMapper.listPageUploadFile(uploadFile,indexPage,limit);
    }

    @Override
    public Integer listPageUploadFileCount(UploadFile uploadFile) {
        return uploadFileMapper.listPageUploadFileCount(uploadFile);
    }

    @Transactional
    @Override
    public boolean delUploadFile(Integer id) {
        try{
            UploadFile uploadFile = uploadFileMapper.selectById(id);
            if (uploadFile==null){
                return false;
            }
            String filePath=uploadFile.getFilePath();
            File file=new File(filePath);
            //如果这个文件物理存在
            if (file.exists()){
                file.delete();//删除文件
            }
            uploadFileMapper.deleteById(id);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transactional
    @Override
    public boolean delUploadFiles(Integer[] ids) {
        try{
            for (Integer id:ids){
                UploadFile uploadFile = uploadFileMapper.selectById(id);
                if (uploadFile==null){
                    continue;
                }
                String filePath=uploadFile.getFilePath();
                File file=new File(filePath);
                //如果这个文件物理存在
                if (file.exists()){
                    file.delete();//删除文件
                }
                uploadFileMapper.deleteById(id);
            }
        }catch (Exception e){
            return false;
        }
        return true;
    }
}
